package com.liujit.pay.wx.domain.params.order;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;

/**
 * @description: 场景信息
 * @author: liujun
 * @create: 2021-02-20 10:59
 */
public class OrderSceneInfo {

    /**
     * 用户终端IP
     */
    private String payerClientIp;

    /**
     * 商户端设备号
     */
    private String deviceId;

    /**
     * 商户门店信息
     */
    private OrderSceneInfo orderStoreInfo;

    OrderSceneInfo(String payerClientIp, String deviceId, OrderSceneInfo orderStoreInfo) {
        this.payerClientIp = payerClientIp;
        this.deviceId = deviceId;
        this.orderStoreInfo = orderStoreInfo;
    }

    /**
     * 初始化参数
     * @param payerClientIp 用户终端IP
     * @return
     */
    public static OrderSceneInfo.Builder builder(String payerClientIp) {
        return new OrderSceneInfo.Builder(payerClientIp);
    }

    public static class Builder {

        private String payerClientIp;
        private String deviceId;
        private OrderSceneInfo orderStoreInfo;

        Builder(String payerClientIp) {
            this.payerClientIp = payerClientIp;
        }

        /**
         * 设置商户端设备号
         * @param deviceId 商户端设备号
         * @return
         */
        public OrderSceneInfo.Builder deviceId(String deviceId) {
            this.deviceId = deviceId;
            return this;
        }

        /**
         * 设置商户门店信息
         * @param orderStoreInfo 商户门店信息
         * @return
         */
        public OrderSceneInfo.Builder orderStoreInfo(OrderSceneInfo orderStoreInfo) {
            this.orderStoreInfo = orderStoreInfo;
            return this;
        }

        public OrderSceneInfo build() {
            return new OrderSceneInfo(this.payerClientIp, this.deviceId, this.orderStoreInfo);
        }
    }

    /**
     * 转化JSON字符串
     *
     * @return
     */
    public String toJSONString() {
        JSONObject json = new JSONObject();
        // 用户终端IP
        json.set("payer_client_ip", payerClientIp);
        // 商户端设备号
        if (StrUtil.isNotBlank(deviceId)) {
            json.set("device_id", deviceId);
        }
        // 商户门店信息
        if (orderStoreInfo != null) {
            json.set("store_info", orderStoreInfo.toJSONString());
        }
        return json.toString();
    }
}
